#ifndef EVENTLOOP_PPOLLER_H
#define EVNETLOOP_PPOLLER_H

#include "Poller.h"

#include <map>
#include <vector>

struct pollfd;

class Ppoller final: public Poller
{
public:
    Ppoller(const Eventloop &loop);
    virtual ~Ppoller() override;

    virtual bool has(const IOchannelProxy &channel) const override;

    /// @param timeout ms
    virtual Utility::timepoint poll(int timeout , IOchannelProxyRefs &activeChannels) override;
    virtual void remove(IOchannelProxy &channel) override;
    virtual void update(IOchannelProxy &channel) override;

private:
    struct _Helper final {
        _Helper(): channel{}, index{1} {}
        _Helper(IOchannelProxy &channel_, std::size_t index_):
            channel{channel_}, index{index_} {}

        const OptionalIOchannelProxyRef channel;
        std::size_t index;
    };

    NONCOPYABLE(Ppoller);

    void _add(IOchannelProxy &channel);
    void _change(const IOchannelProxy &channel);
    virtual void _fill_active_channels(int numEvents, IOchannelProxyRefs &activeChannels) const override;
    bool _is_in_channels(int fd) const;
    void _swap_back(const IOchannelProxy &channel);
   
    std::map<int, _Helper> _fdMap;
    std::vector<struct pollfd> _pollfds;
};



#endif